package mi.payment.settings;

import mi.payment.utils.SharedPreferencesUtil;

import static mi.payment.settings.TransactionInputPwd.TRANSACTION_INPUT_PSD;

/**
 * 交易输密控制
 *
 * @author sunmi on 2017/2/10.
 */

public class TransactionInputPwdOperation {

    private static TransactionInputPwdOperation instance;
    private TransactionInputPwd instanceSettings;

    private TransactionInputPwdOperation() {
        instanceSettings = getTransactionInputPwd();
        if (instanceSettings == null) {
            instanceSettings = new TransactionInputPwd();
        }
        saveTransactionInputPwd(instanceSettings);
    }

    public static TransactionInputPwdOperation getInstance() {
        if (instance == null) {
            instance = new TransactionInputPwdOperation();
        }
        return instance;
    }

    public void init(boolean consumptionRevokePsd,
                     boolean fieldAuthRevokePsd,
                     boolean fieldAuthCompleteRevokePsd,
                     boolean fieldAuthCompleteReqPsd) {
        TransactionInputPwd bean = getTransactionInputPwd();
        bean.setConsumptionRevokePsd(consumptionRevokePsd);
        bean.setFieldAuthRevokePsd(fieldAuthRevokePsd);
        bean.setFieldAuthCompleteRevokePsd(fieldAuthCompleteRevokePsd);
        bean.setFieldAuthCompleteReqPsd(fieldAuthCompleteReqPsd);
        saveTransactionInputPwd(bean);
    }

    public void clearTransactionInputPsd() {
        SharedPreferencesUtil.clearByKey(TRANSACTION_INPUT_PSD);
    }

    /**
     * 得到交易输密的密码
     */
    public TransactionInputPwd getTransactionInputPwd() {
        if(instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(TRANSACTION_INPUT_PSD);
            if (obj instanceof TransactionInputPwd) {
                instanceSettings = (TransactionInputPwd) obj;
            } else {
                instanceSettings = new TransactionInputPwd();
                saveTransactionInputPwd(instanceSettings);
            }
        }
        return instanceSettings;
    }

    /**
     * 保存消费撤销密码
     */
    public void saveConsumptionRevokePwd(boolean consumptionRevokePsd) {
        TransactionInputPwd inputPwd = getTransactionInputPwd();
        inputPwd.setConsumptionRevokePsd(consumptionRevokePsd);
        saveTransactionInputPwd(inputPwd);
    }

    /**
     * 保存域授权撤销密码
     */
    public void saveFieldAuthRevokePwd(boolean fieldAuthRevokePsd) {
        TransactionInputPwd inputPwd = getTransactionInputPwd();
        inputPwd.setFieldAuthRevokePsd(fieldAuthRevokePsd);
        saveTransactionInputPwd(inputPwd);
    }

    /**
     * 保存域授权完成撤销密码
     */
    public void saveFieldAuthCompleteRevokePwd(boolean fieldAuthCompleteRevokePsd) {
        TransactionInputPwd inputPwd = getTransactionInputPwd();
        inputPwd.setFieldAuthCompleteRevokePsd(fieldAuthCompleteRevokePsd);
        saveTransactionInputPwd(inputPwd);
    }

    /**
     * 保存域授权完成（请求）密码
     */
    public void saveFieldAuthCompleteReqPwd(boolean fieldAuthCompleteReqPsd) {
        TransactionInputPwd inputPwd = getTransactionInputPwd();
        inputPwd.setFieldAuthCompleteReqPsd(fieldAuthCompleteReqPsd);
        saveTransactionInputPwd(inputPwd);
    }

    /**
     * 保存交易输密的密码
     */
    public void saveTransactionInputPwd(TransactionInputPwd transactionInputPsd) {
        this.instanceSettings = transactionInputPsd;
        SharedPreferencesUtil.saveObj(transactionInputPsd, TRANSACTION_INPUT_PSD);
    }

}
